package com.bookmanger.server.service.impl;

import com.bookmanger.server.service.EmailService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class EmailServiceImpl implements EmailService {
    @Value("${spring.mail.username}")
    private String fromEmail;

    @Autowired
    private JavaMailSender mailSender;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public boolean sendEmailCode(String email) {
        // 生成验证码
        String code = String.format("%06d", new Random().nextInt(999999));

        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(fromEmail);
            helper.setTo(email);
            helper.setSubject("您的验证码");
            helper.setText(String.format("您的验证码是：%s，请在5分钟内使用。", code));

            // 发送邮件
            mailSender.send(message);

            // 存储验证码到 Redis 中
            ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
            ops.set("email:code:" + email, code, 5, TimeUnit.MINUTES);

            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean verifyEmailCode(String email, String code) {
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String cachedCode = ops.get("email:code:" + email);
        return cachedCode != null && cachedCode.equals(code);
    }

    public boolean sendEmailSuccess(String email) {
        // 生成验证码
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(fromEmail);
            helper.setTo(email);
            helper.setSubject("申请通过");
            helper.setText(String.format("您在极创未来图书馆的注册申请已通过"));

            // 发送邮件
            mailSender.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean sendEmailUnsuccess(String email, String reason) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(fromEmail);
            helper.setTo(email);
            helper.setSubject("申请未通过");
            helper.setText(String.format("您在极创未来图书馆的注册申请未通过,原因如下:\n%s", reason));

            // 发送邮件
            mailSender.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public boolean sendEmailMessage(String email) {
        // 生成验证码
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);

            helper.setFrom(fromEmail);
            helper.setTo(email);
            helper.setSubject("申请通过");
            helper.setText(String.format("您在极创未来图书馆的注册申请已通过"));

            // 发送邮件
            mailSender.send(message);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean sendEmail(String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            
            helper.setFrom(fromEmail);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            
            mailSender.send(message);
            return true;
        } catch (MessagingException e) {
            log.error("发送邮件失败，收件人：{}，主题：{}", to, subject, e);
            return false;
        }
    }
}